package com.hod.client.roomtype;

import com.google.gwt.user.client.Window;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.VType;
import com.hod.client.HOD;
import com.hod.client.backend.orm.client.GearsDataObject;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.frontend.HODForm;

public class RoomTypeFormPanel extends HODForm {
	
	protected final TextField txtIdRoomType = new TextField("idRoomType", "idRoomType", 150);
    protected final TextField txtName = new TextField("name", "name", 150);
    protected final NumberField txtMaxPax = new NumberField("maxPax", "maxPax", 150);
    protected final NumberField txtPrice = new NumberField("price", "price", 150);
    protected final TextField txtBed = new TextField("bed", "bed", 150);
    protected final NumberField txtnumberOfRooms = new NumberField("numberOfRooms", "numberOfRooms", 150);


	public RoomTypeFormPanel(GearsDataStore ds) {
		super(ds);
		// TODO Auto-generated constructor stub
		add(createFieldSet());        
        createButtons();
	}

	@Override
	protected void createButtons() {
		ButtonListenerAdapter savelistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		RoomType o = null;
            		if (txtIdRoomType.getText()==""){
            			o = new RoomType();
            			o.setIdRoomType(null);
            		 }else{
            			o = (RoomType) ds.findById(new Integer(txtIdRoomType.getText()));
            		 }                		
                    o.setName(txtName.getValueAsString());
                    o.setMaxPax((Integer)txtMaxPax.getValue());
                    o.setPrice(Double.valueOf(txtPrice.getValueAsString()));
                    o.setBed(txtBed.getValueAsString());
                    o.setNumberOfRooms((Integer)txtnumberOfRooms.getValue());
                    o.setActive(new Boolean(true));
                    ds.save(o);
                    HOD.reload();
				} catch (Exception e1) {
						// TODO Auto-generated catch block
						Window.alert(e1.getMessage());
					}
            }
        };
        ButtonListenerAdapter deletelistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		RoomType rt = (RoomType) ds.findById(new Integer(txtIdRoomType.getText()));
            		ds.delete(rt);
            		HOD.reload();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						Window.alert(e1.getMessage());
					}
            }
        };
        ButtonListenerAdapter createlistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		getForm().reset();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						Window.alert(e1.getMessage());
					}
            }
        };
        	           
        
        Button save = new Button("Save", savelistener);
        Button delete = new Button("Delete", deletelistener);
		 Button newRecord = new Button("New", createlistener);
        Button cancel = new Button("Cancel");

        addButton(save);
        addButton(delete);
        addButton(cancel);
        addButton(newRecord);

	}

	@Override
	protected FieldSet createFieldSet() {
		// TODO Auto-generated method stub
		 FieldSet fieldSet = new FieldSet();
	        fieldSet.setLabelWidth(80);
	        fieldSet.setTitle("RoomType");
	        fieldSet.setAutoHeight(true);
	        fieldSet.setBorder(false);
	        

	        //the field names msut match the data field values from the Store
	        fieldSet.add(txtIdRoomType);
	        fieldSet.add(txtName);            
	        fieldSet.add(txtMaxPax);
	        fieldSet.add(txtPrice);
	        fieldSet.add(txtBed);
	        fieldSet.add(txtnumberOfRooms);     
	        
	        txtIdRoomType.setVisible(false);
	        txtIdRoomType.setHideLabel(true);
	        
	        txtName.setAllowBlank(false);
	        txtName.setVtype(VType.ALPHA);
	        
	        txtMaxPax.setAllowBlank(false);
	        txtMaxPax.setAllowDecimals(false);
	        txtMaxPax.setAllowNegative(false);
	        txtMaxPax.setDecimalPrecision(0);
	        
	        txtPrice.setAllowDecimals(true);
	        txtPrice.setAllowNegative(false);
	        txtPrice.setDecimalPrecision(2);
	        
	        return fieldSet;
	}

	@Override
	public void updateDataControls(Record record) {
		getForm().loadRecord(record); 

	}

	@Override
	public void setObject(GearsDataObject object) {
		// TODO Auto-generated method stub
		
	}

}
